package pro.bbs.view.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yy.constant.AppConstant;
import com.yy.kit.collections.YYMapKits;
import com.yy.pojo.bbs.TPost;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import pro.bbs.view.service.TPostRemoteService;
import pro.bbs.view.web.base.BbsCommonController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 论坛帖子相关控制类
 * </p>
 *
 * @author 杨阳
 * @since 2017年10月12日
 */
@Api(value = "论坛帖子相关控制类", tags = {"包含发帖，回帖等操作,请求根路径：/post"})
@RestController
@RequestMapping("/post")
public class BbsPostController extends BbsCommonController {

    @Autowired
    private TPostRemoteService tPostRemoteService;

    @ApiOperation(value = "查询出所有帖子，以发贴时间倒序排序", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "page", required = true),
            @ApiImplicitParam(value = "每页显示记录数", name = "limit", required = true),
            @ApiImplicitParam(value = "查询帖子标题", name = "title", example = "JAVA"),
            @ApiImplicitParam(value = "查询帖子内容", name = "content", example = "如何发帖")
    })
    @RequestMapping(value = "/getAllPosts", method = RequestMethod.GET)
    //@HystrixCommand(fallbackMethod = "getAllPostsFallMethod")解释：
    //表示使用断路器，如果执行失败，调用getAllPostsFallMethod方法。
    //@HystrixCommand(fallbackMethod = "getAllPostsFallMethod", commandProperties = {
            //设置断路器隔离策略为线程策略，（默认即为这种策略）
            //如果发生找不到上下文的运行时异常，可考试将隔离策略设置为SEMAPHORE
            //@HystrixProperty(name = AppConstant.HYSTRIX_STRATEGY_CLASS, value = AppConstant.HYSTRIX_STRATEGY_THREAD)
    //})
    public List<TPost> getAllPosts2(Integer page, Integer limit, String title, String content) {



        Map<String, Object> uriVariables = YYMapKits.createHashMap();
        uriVariables.put("page", page);
        uriVariables.put("limit", limit);
        uriVariables.put("title", title);
        uriVariables.put("content", content);

        return tPostRemoteService.getAllPosts(uriVariables);
    }

}
